package com.cosina.game.tank.model;

import android.graphics.Bitmap;
import android.graphics.Point;
import android.graphics.Rect;

import com.cosina.game.tank.util.Direction;
import com.cosina.game.tank.util.DirectionAdapter;

public abstract class Tank extends LifeMatter{
	
	private final Point location;
	public final War war;
	public Direction direction =  Direction.up;
	
	public Tank(Point location, War war){
		this.location = location;
		this.war = war;
	}

	public abstract int getSpeed();
	
	public Rect getArea(){
		return new Rect(location.x,location.y, location.x + 32,location.y + 32);
	}
	
	public Rect getBodyArea(){
		return new Rect(location.x+8,location.y+8, location.x + 24,location.y + 24);
	}
	
	
	public int getX(){ return this.location.x;}
	public int getY(){ return this.location.y;}
	
	public void offset(int xOffset, int yOffset) {
		this.location.offset(xOffset, yOffset);
	}
	
	public final Point getCenterLocation(){
		Point result = new Point(location);
		result.offset(16, 16);
		return result;
	}
	
	public final void shot(){
		war.pills.add(new Pill(getCenterLocation(),this.direction,isEvil()));
	}
	
	public abstract boolean isEvil();
	public abstract DirectionAdapter getImageAdapter();
	public final Bitmap getImage(){
		return (Bitmap)getImageAdapter().doSwtich(direction);
	}
	
	
	public DirectionAdapter moveDirectionAdapter = new DirectionAdapter(){
		@Override
		public Object doDown() {
			war.tryMove(Tank.this, 0, getSpeed());
			direction = Direction.down;
			return null;
		}

		@Override
		public Object doLeft() {
			war.tryMove(Tank.this, -getSpeed(),0);
			direction = Direction.left;
			return null;
		}

		@Override
		public Object doRight() {
			war.tryMove(Tank.this, getSpeed(),0);
			direction = Direction.right;
			return null;
		}

		@Override
		public Object doUp() {
			war.tryMove(Tank.this, 0,-getSpeed());
			direction = Direction.up;
			return null;
		}
		
	};
}
